<!DOCTYPE html>
<!--suppress ALL -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>

</head>
<body>
<div id="vuebox">
    <div class="row">
        <div class="col-md-6">
            <form class="form-inline" role="form" style="padding-left: 250px;">
                <div class="form-group" style="padding-left: 20px;">
                    <label>新闻分类:</label>
                    <select class="form-control" v-model="sortId">
                        <option value="0">全部</option>
                        <option v-for="item in sortList.data" :value="item.id">{{item.name}}</option>
                    </select>
                    <label>新闻标题:</label>
                    <input v-model="title"  type="text" class="form-control">
                </div>
                <div class="form-group"  style="padding-left: 20px;">
                    <button type="button" class="btn btn-primary" @click="selectDetail(1)">查询</button>
                </div>
            </form>
        </div>
    </div>
    <br>
    <table class="table table-bordered container">
        <thead>
        <tr>
            <th>新闻编号</th>
            <th>新闻标题</th>
            <th>新闻摘要</th>
            <th>作者</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in detailList.data">
            <td>{{item.id}}</td>
            <td>{{item.title}}</td>
            <td>{{item.detail}}</td>
            <td>{{item.author}}</td>
            <td>{{item.createdate}}</td>
            <td>
                <button class="btn btn-success" @click="updateEntry(item.id)">修改</button>
            </td>
        </tr>
        </tbody>
    </table>
    <br>

    <!--模态框-->
    <div class="modal" id="mymodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" >&times;</button>
                    <div class="modal-title">
                        添加图书
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <label for="Id" class="col-sm-2 control-label">新闻编号:</label>
                        <input type="text" id="Id" v-model="new_detail.id">
                        <div class="form-group">
                            <label for="categoryId" class="col-sm-2 control-label">新闻分类:</label>
                            <div class="col-sm-10">
                                <select id="sortId" class="form-control" v-model="new_detail.sortid">
                                    <option value="0">全部</option>
                                    <option v-for="item in sortList.data" :value="item.id">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="title" class="col-sm-2 control-label">新闻标题:</label>
                            <div class="col-sm-10">
                                <input v-model="new_detail.title" id="title" type="text" class="form-control" placeholder="请输入新闻标题">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="detail" class="col-sm-2 control-label">新闻摘要:</label>
                            <div class="col-sm-10">
                                <input v-model="new_detail.detail" id="detail" type="text" class="form-control" placeholder="请输入新闻摘要"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="author" class="col-sm-2 control-label">作者:</label>
                            <div class="col-sm-10">
                                <input v-model="new_detail.author" id="author" type="text" class="form-control" placeholder="请输入作者">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="createdate" class="col-sm-2 control-label">创建时间</label>
                            <div class="col-sm-10">
                                <input v-model="new_detail.createdate" id="createdate" type="text" class="form-control" placeholder="请输入创建时间">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" @click="hideModal(),clearForm()">关闭</button>
                    <button class="btn btn-primary" @click="updateModel()">更新</button>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
</html>
<script type="text/javascript">
    var vm=new Vue({
        el:"#vuebox",
        data:{
            sortList:{},
            sortId:0,
            title:"",
            detailList:{},
            updateResult:{},
            new_detail:{id:null,sortid:'0',title:'',detail:'',author:'',createdate:null}
        },
        methods:{
            selectDetail:function () {
                axios.get("/selectDetail",{params:{title:this.title,sortid:this.sortId}}).then(
                    res=>{
                        this.detailList=res.data
                    }
                )
            },
            showModal:function () {
                $('#mymodal').modal('show');
            },
            hideModal:function () {
                $('#mymodal').modal('hide');
            },
            clearForm:function () {
                $('#Id').val('');
                $('#sortid').val(0);
                $('#title').val('');
                $('#detail').val('');
                $('#author').val('');
                $('#createdate').val('');
            },
            updateEntry:function (id) {
                axios.get("/getDetailById",{params:{id:id}}).then(res=>{
                    this.new_detail=res.data.data;
                    this.showModal();
                })
            },
            updateModel:function () {
                axios.post("/updateDetail",this.new_detail).then(res=>{
                    this.updateResult=res.data;
                    if (this.updateResult.result=="SUCCESS"){
                        this.selectDetail(1)
                        this.hideModal();
                        this.clearForm();
                    }else{
                        alert(this.delResult.message)
                    }

                })
            }
        },
        mounted(){
            axios.get("/getSortList").then(res=>this.sortList=res.data)
            this.selectDetail(1);
        }
    });
</script>